package functionutil

import (
	"encoding/json"
	"gitee.com/cjf8134/douyin/utils/constutil"
	"gitee.com/cjf8134/douyin/utils/curlutil"
	"gitee.com/cjf8134/douyin/utils/types"
	"io"
	"io/ioutil"
	"log"
	"os"
)

// RequestClient 客户端请求
func RequestClient(url string) (result types.ResponseData, err error) {
	resp, err := curlutil.HttpCurl("GET", url, nil, nil)
	if err != nil {
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	err = json.Unmarshal(body, &result)
	if err != nil {
		return
	}
	return
}

// VideoDownload  下载视频
func VideoDownload(video types.VideoList) (err error) {
	resp, err := curlutil.HttpCurl("GET", video.Url, nil, nil)
	if err != nil {
		return
	}
	log.Println(constutil.DownloadPath + video.AwemeId + ".mp4")
	f, err := os.Create(constutil.DownloadPath + video.AwemeId + ".mp4")
	if err != nil {
		return
	}
	_, err = io.Copy(f, resp.Body)
	if err != nil {
		return
	}
	return
}
